package com.action.music;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.music.MusicInfo;
import com.business.music.MusicInfoBus;
import com.config.SystemGlobals;
import com.jspsmart.upload.SmartUpload;
import com.util.MD5;
import com.util.TextTools;


public class AddMusic extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
		Log log = LogFactory.getLog(AddMusic.class);
		String musicUrl="";
		String lrcUrl="";
		SmartUpload SmartUpload = new SmartUpload();
		SmartUpload.initialize(getServletConfig(), request, response);
		SmartUpload.setTotalMaxFileSize(0xA00000L);
		try {
			SmartUpload.upload();
		} catch (Exception ex) {
			log.error("smartUpload upload():" + ex.getMessage());
		}

		String startpath = SystemGlobals.getValue("file_upload_path");
		startpath=startpath+"/music";
		String filepath = "";
		try {
			for (int i = 0; i < SmartUpload.getFiles().getCount(); i++) {
				com.jspsmart.upload.File myfile = SmartUpload.getFiles()
						.getFile(i);
				String imagefile = myfile.getFileName();
				if (!myfile.isMissing()) {
					MD5 md5 = new MD5();
					String md5str = md5.getMD5ofStr(myfile.getContentString());
					imagefile = md5str + "." + myfile.getFileExt();
					myfile.saveAs(startpath
							+ TextTools.mkHashDir(startpath, md5str) + "/"
							+ imagefile);
					filepath = "/upload/music"
							+ TextTools.mkHashDir(startpath, md5str) + "/"
							+ imagefile;
					if(i==0) {
						musicUrl = filepath;
					}
					if(i==1){
					lrcUrl=filepath;
					}
				}
			}
		} catch (Exception se) {
			log.error("the upload File" + filepath + " is Failed "
					+ se.toString());
		}
		
		Date date = new Date();
		Timestamp ts = new Timestamp(date.getTime());
		SimpleDateFormat sdfNo = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String musicName = SmartUpload.getRequest().getParameter("musicname");
		String musicMemo = SmartUpload.getRequest().getParameter("musicmemo");
		String username=SmartUpload.getRequest().getParameter("username");
		String musictypeId = SmartUpload.getRequest().getParameter(
				"musictypeid");
		String isShow = SmartUpload.getRequest().getParameter("isshow");
		String className=SmartUpload.getRequest().getParameter("classname");
		String singer=SmartUpload.getRequest().getParameter("singer");
		MusicInfo mi = new MusicInfo();
		mi.setAddTime(sdf.format(ts));
		mi.setUsername(username);
		mi.setIsShow(isShow);
		mi.setMusicMemo(musicMemo);
		mi.setMusicName(musicName);
		mi.setMusicNo(sdfNo.format(ts));
		mi.setMusictypeId(musictypeId);
		mi.setClassName(className);
		mi.setSinger(singer);
		mi.setMusicUrl(musicUrl);
		mi.setLrcUrl(lrcUrl);
		mi.setViewCount("0");
		MusicInfoBus mib = new MusicInfoBus();
		mib.add(mi);
		out.println("<script>alert(\"您成功上传了一首音乐\");history.back(-1);</script>");
	}

}
